﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using EBookReader.Library;
using EBookReader.Library.Config;
using EBookReader.Library.Data;
using EBookReader.Library.Encoder;

namespace EBookReader.Client
{
    class MainClient
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> command = new Dictionary<string, string>();
            if (args.Length < 1)
            {
                Console.Write(Help.MainTitle);
                Console.WriteLine("按任意键退出.");
                Console.ReadKey();
                Environment.Exit(0);
            }
            string temp = string.Empty;
            bool isValue = false;
            for (int i = 0; i < args.Length; i++)
            {
                if (!isValue)
                {
                    if (args[i].StartsWith("-"))
                    {
                        isValue = true;
                        temp = args[i].Substring(1);
                        temp = temp.ToLower();
                        if (temp == "help")
                        {
                            Console.Write(Help.MainHelp);
                            Environment.Exit(0);
                        }
                    }
                }
                else
                {
                    if (!command.ContainsKey(temp))
                    {
                        command.Add(temp, args[i]);
                    }
                    isValue = false;
                }
            }
            if (command.ContainsKey("add"))
            {
                AddWebSite(command["add"]);
            }
            else
            {
                if (!command.ContainsKey("get") || !command.ContainsKey("config"))
                {
                    Console.WriteLine("您至少要提供-get和-config参数.");
                    Environment.Exit(0);
                }
                string saveName;
                if (command.ContainsKey("save"))
                    saveName = command["save"];
                else
                    saveName = string.Empty;
                string typeName = "all";
                if (command.ContainsKey("type"))
                    typeName = command["type"];
                if (typeName != "all" && typeName != "info" && typeName != "chapter" && typeName != "xml")
                {
                    Console.WriteLine("错误的-type参数,请使用-help查看系统帮助.");
                    Environment.Exit(0);
                }
                if (typeName == "xml")
                {
                    if (!command.ContainsKey("xml"))
                    {
                        Console.WriteLine("-type xml选项必须提供xml索引文件.");
                        Environment.Exit(0);
                    }
                }

            }
            foreach (KeyValuePair<string, string> kv in command)
            {
                Console.WriteLine(string.Format("{0} = {1}", kv.Key, kv.Value));
            }
        }

        public static void AddWebSite(string name)
        {
            EBookWebsite website = new EBookWebsite();
            Console.WriteLine("站点名:");
            website.Name = Console.ReadLine();
            Console.WriteLine("站点简介:");
            website.Intro = Console.ReadLine();
            Console.WriteLine("站点URL:");
            website.Url = Console.ReadLine();
            Console.WriteLine("站点编码:");
            website.Encode = Console.ReadLine();
            Console.WriteLine("是否启用GZip?(t/f)");
            string result = Console.ReadLine();
            while (result != "t" && result != "f")
            {
                Console.WriteLine("是否启用GZip?");
                result = Console.ReadLine();
            }
            if (result == "t")
                website.Type = "GZIP";
            else
                website.Type = "NONE";
            Console.WriteLine("小说信息匹配正则表达式:");
            website.InfoRegex = Console.ReadLine();
            Console.WriteLine("小说章节列表匹配正则表达式:");
            website.TitleRegex = Console.ReadLine();
            Console.WriteLine("小说章节列表去除版权信息正则表达式:");
            result = Console.ReadLine();
            if (!string.IsNullOrEmpty(result))
            {
                website.TitleEncodeRegex.Add(result);
            }
            Console.WriteLine("小说章节匹配正则表达式:");
            website.ContentRegex = Console.ReadLine();
            Console.WriteLine("小说章节去除版权信息正则表达式:");
            result = Console.ReadLine();
            if (!string.IsNullOrEmpty(result))
            {
                website.ContentEncodeRegex.Add(result);
            }
            try
            {
                FileStream fs = new FileStream(name + ".config", FileMode.OpenOrCreate);
                XmlSerializer xs = new XmlSerializer(typeof(EBookWebsite));
                xs.Serialize(fs, website);
                fs.Close();
                Console.WriteLine("新的配置文件创建成功.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误:" + ex.Message);
            }
            Environment.Exit(0);
        }

        public static EBookInfo GetBookInfo(string url)
        {
            return null;
        }

        public static BookChapter GetChapter(string url)
        {
            return null;
        }

        public static void SaveChapter(string url, string file)
        {
        }

        public static void SaveBookXml(string inFile, string outFile)
        {
        }

        public static void SaveBookInfo(string url, string file)
        {
        }

        public static void SaveAll(string url, string file)
        {
        }
    }
}
